package se.webbzon.boltzmann.game.sequence.builder.event;

import se.webbzon.boltzmann.game.event.GameEventEnum;
import se.webbzon.boltzmann.game.sequence.GameEventSequenceNode;
import se.webbzon.boltzmann.game.sequence.Sequence;
import se.webbzon.boltzmann.game.sequence.SequenceNode;
import se.webbzon.boltzmann.game.sequence.builder.AbstractSequenceRow;
import se.webbzon.boltzmann.game.sequence.builder.SequenceBuilder;
import se.webbzon.boltzmann.game.sequence.builder.SequenceRowDeflation;
import se.webbzon.oschi01.worldarea.WorldArea;

public class GameEventRow extends AbstractSequenceRow {

	// The name of the sequence row
	public static String name = "gameEvent";
	
	// The game event enumeration of this game event row
	private GameEventEnum gameEvent;
	
	// The arguments of this game event row
	private String[] arguments;

	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new game event row with a given game event enumeration. **/
	public GameEventRow(SequenceBuilder builder, 
						GameEventEnum gameEvent) {
		this(builder,gameEvent,null);
	}

	/** Creates a new game event row with a given game event enumeration
	 * and a given set of arguments. **/
	public GameEventRow(SequenceBuilder builder, 
						GameEventEnum gameEvent, 
						String[] arguments) {
		super(name, builder);
		this.gameEvent = gameEvent;
		this.arguments = arguments;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the game event enumeration of this game event row. **/
	public GameEventEnum getGameEvent() {
		return gameEvent;
	}
	
	/** Sets the game event enumeration of this game event row. **/
	public void setGameEvent(GameEventEnum gameEvent) {
		this.gameEvent = gameEvent;
	}
	
	/** Returns the arguments of this game event row as an array
	 * of strings. **/
	public String[] getArguments() {
		return arguments;
	}
	
	/** Sets the arguments of this game event row from an array
	 * of strings. **/
	public void setArguments(String[] arguments) {
		this.arguments = arguments;
	}
	
	@Override public String getText() {
		return "event(" + gameEvent.name() + ")";
	}

	@Override public int maxLinks() {
		return 1;
	}

	@Override public int minLinks() {
		return 0;
	}

	@Override public SequenceNode compile(WorldArea world, Sequence sequence) {
		return new GameEventSequenceNode(sequence,gameEvent,arguments);
	}

	@Override public void bind(SequenceNode node, SequenceNode[] nodes) {
		GameEventSequenceNode gameEventNode = 
				(GameEventSequenceNode) node;
		if (links() == 1) { 
			Integer branch = getLink(0);
			if (branch != null)
				gameEventNode.setBranch(nodes[getLink(0)]);
		}
	}

	@Override public SequenceRowDeflation getDeflation() {
		return new GameEventRowDeflation(this);
	}

}
